# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/21 17:12
# @Author   : Robot-Zsj
"""
description:

i:
2,2
10,20
30,40

o:
70
"""
import sys

m, n = map(int, input().split(","))

matrix = [list(map(int, input().split(","))) for _ in range(m)]

offsets = ((-1, 0), (0, -1), (1, 0), (0, 1))


class Node:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.init = 0  # 到达此位置所需的最少初始油量
        self.remain = 0  # 到达此位置剩余可用油量
        self.flag = False  # 到达此位置是否加油


def bfs():
    if matrix[0][0] == 0 or matrix[m - 1][n - 1] == 0:
        return -1

    queue = []

    src = Node(0, 0)

    if matrix[0][0] == -1:
        src.init = 0
        src.remain = 100
        src.flag = True

    else:
        src.init = matrix[0][0]
        src.remain = 0
        src.flag = False

    queue.append(src)
    dist_init = [[sys.maxsize] * n for _ in range(m)]

    dist_remain = [[0] * n for _ in range(m)]

    dist_init[0][0] = src.init
    dist_remain[0][0] = src.remain

    while len(queue) > 0:
        cur = queue.pop(0)

        for offsetX, offsetY in offsets:
            newX = cur.x + offsetX
            newY = cur.y + offsetY

            if newX < 0 or newX >= m or newY < 0 or newY >= n or matrix[newX][newY] == 0:
                continue

            init = cur.init
            remain = cur.remain
            flag = cur.flag

            if matrix[newX][newY] == -1:
                remain = 100
                flag = True
            else:
                remain -= matrix[newX][newY]

            if remain < 0:
                if flag:
                    continue
                else:
                    init -= remain
                    remain = 0

            if init > 100:
                continue

            if init > dist_init[newX][newY]:
                continue

            if init < dist_init[newX][newY] or remain > dist_remain[newX][newY]:
                dist_init[newX][newY] = init
                dist_remain[newX][newY] = remain

                nxt = Node(newX, newY)
                nxt.init = init
                nxt.remain = remain
                nxt.flag = flag

                queue.append(nxt)

    if dist_init[m - 1][n - 1] == sys.maxsize:
        return -1
    else:
        return dist_init[m - 1][n - 1]


print(bfs())
